<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{assign}</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="language.builtin.functions.html" title="Chapter 7. Built-in Functions">
<link rel="prev" href="language.function.append.html" title="{append}">
<link rel="next" href="language.function.block.html" title="{block}">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">{assign}</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.function.append.html">Prev</a> </td>
<th width="60%" align="center">Chapter 7. Built-in Functions</th>
<td width="20%" align="right"> <a accesskey="n" href="language.function.block.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="{assign}">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.function.assign"></a>{assign}</h2></div></div></div>
<p>
   <code class="varname">{assign}</code> is used for assigning template variables
   <span class="bold"><strong>during the execution  of a template</strong></span>.
 </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
   Assignment of variables in-template is essentially placing application logic into the presentation that may be better handled in PHP. Use at your own discretion.
 </p>
</div>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
    See also the <a class="link" href="language.builtin.functions.html#language.function.shortform.assign" title="{$var=...}"><code class="varname">short-form</code></a> 
    method of assigning template vars.
 </p>
</div>
<p><span class="bold"><strong>Attributes:</strong></span></p>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col align="center">
<col align="center">
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Attribute Name</th>
<th align="center">Type</th>
<th align="center">Required</th>
<th align="center">Default</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr>
<td align="center">var</td>
<td align="center">string</td>
<td align="center">Yes</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>The name of the variable being assigned</td>
</tr>
<tr>
<td align="center">value</td>
<td align="center">string</td>
<td align="center">Yes</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>The value being assigned</td>
</tr>
<tr>
<td align="center">scope</td>
<td align="center">string</td>
<td align="center">No</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>The scope of the assigned variable: 'parent','root' or 'global'</td>
</tr>
</tbody>
</table></div>
<p><span class="bold"><strong>Option Flags:</strong></span></p>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Name</th>
<th>Description</th>
</tr></thead>
<tbody><tr>
<td align="center">nocache</td>
<td>Assigns the variable with the 'nocache' attribute</td>
</tr></tbody>
</table></div>
<div class="example">
<a name="id365553"></a><p class="title"><b>Example 7.8. {assign}</b></p>
<div class="example-contents">
<pre class="programlisting">

{assign var="name" value="Bob"}
{assign "name" "Bob"} {* short-hand *}

The value of $name is {$name}.

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

The value of $name is Bob.

  </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id365578"></a><p class="title"><b>Example 7.9. {assign} as a nocache variable</b></p>
<div class="example-contents">
<pre class="programlisting">

{assign var="name" value="Bob" nocache}
{assign "name" "Bob" nocache} {* short-hand *}

The value of $name is {$name}.

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

The value of $name is Bob.

  </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id365603"></a><p class="title"><b>Example 7.10. {assign} with some maths</b></p>
<div class="example-contents"><pre class="programlisting">

{assign var=running_total value=$running_total+$some_array[$row].some_value}

  </pre></div>
</div>
<br class="example-break"><div class="example">
<a name="id365617"></a><p class="title"><b>Example 7.11. {assign} in the scope of calling template</b></p>
<div class="example-contents">
<p>Variables assigned in the included template will be seen in the including template.</p>
<pre class="programlisting">

{include file="sub_template.tpl"}
...
{* display variable assigned in sub_template *}
{$foo}&lt;br&gt;
...

  </pre>
<p>The template above includes the example <code class="filename">sub_template.tpl</code> below</p>
<pre class="programlisting">

...
{* foo will be known also in the including template *}
{assign var="foo" value="something" scope=parent}
{* bar is assigned only local in the including template *}
{assign var="bar" value="value"}
...

</pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id365650"></a><p class="title"><b>Example 7.12. {assign} a variable to current scope tree</b></p>
<div class="example-contents">
<p>You can assign a variable to root of the current root tree. The variable is seen by all templates using the same root tree.</p>
<pre class="programlisting">

{assign var=foo value="bar" scope="root"}

  </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id365673"></a><p class="title"><b>Example 7.13. {assign} a global variable</b></p>
<div class="example-contents">
<p>A global variable is seen by all templates.</p>
<pre class="programlisting">

{assign var=foo value="bar" scope="global"}
{assign "foo" "bar" scope="global"} {* short-hand *}

  </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id365691"></a><p class="title"><b>Example 7.14. Accessing {assign} variables from a PHP script</b></p>
<div class="example-contents">
<p>
  To access <code class="varname">{assign}</code> variables from a php script use
    <a class="link" href="api.get.template.vars.html" title="getTemplateVars()">
    <code class="varname">getTemplateVars()</code></a>.
    Here's the template that creates the variable <em class="parameter"><code>$foo</code></em>.
  </p>
<pre class="programlisting">

{assign var="foo" value="Smarty"}

</pre>
<p>The template variables are only available after/during template
execution as in the following script.
</p>
<pre class="programlisting">

&lt;?php

// this will output nothing as the template has not been executed
echo $smarty-&gt;getTemplateVars('foo');

// fetch the template to a variable
$whole_page = $smarty-&gt;fetch('index.tpl');

// this will output 'smarty' as the template has been executed
echo $smarty-&gt;getTemplateVars('foo');

$smarty-&gt;assign('foo','Even smarter');

// this will output 'Even smarter'
echo $smarty-&gt;getTemplateVars('foo');

?&gt;

</pre>
</div>
</div>
<br class="example-break"><p>
 The following functions can also <span class="emphasis"><em>optionally</em></span> assign
 template variables.
  </p>
<p>
  <a class="link" href="language.function.capture.html" title="{capture}"><code class="varname">{capture}</code></a>,
  <a class="link" href="language.function.include.html" title="{include}"><code class="varname">{include}</code></a>,
  <a class="link" href="language.function.include.php.html" title="{include_php}"><code class="varname">{include_php}</code></a>,
  <a class="link" href="language.function.insert.html" title="{insert}"><code class="varname">{insert}</code></a>,
 <a class="link" href="language.custom.functions.html#language.function.counter" title="{counter}"><code class="varname">{counter}</code></a>,
 <a class="link" href="language.function.cycle.html" title="{cycle}"><code class="varname">{cycle}</code></a>,
 <a class="link" href="language.function.eval.html" title="{eval}"><code class="varname">{eval}</code></a>,
 <a class="link" href="language.function.fetch.html" title="{fetch}"><code class="varname">{fetch}</code></a>,
 <a class="link" href="language.function.math.html" title="{math}"><code class="varname">{math}</code></a>,
 <a class="link" href="language.function.textformat.html" title="{textformat}"><code class="varname">{textformat}</code></a>
 </p>
<p>
 See also
  <a class="link" href="language.builtin.functions.html#language.function.shortform.assign" title="{$var=...}"><code class="varname">{$var=...}</code></a>,
 <a class="link" href="api.assign.html" title="assign()"><code class="varname">assign()</code></a>
   and
  <a class="link" href="api.get.template.vars.html" title="getTemplateVars()"><code class="varname">getTemplateVars()</code></a>.
 </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.function.append.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="language.builtin.functions.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.function.block.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">{append} </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> {block}</td>
</tr>
</table>
</div>
</body>
</html>
